
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmabbe.common.global.query.GeneralQuery;
import com.lmabbe.common.global.result.R;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* ${tableComment!''}管理
*
* @author lmabbe
*/
@Api(tags = "${tableComment!''}管理")
@RestController
@RequestMapping("${tableName1}")
@RequiredArgsConstructor
public class ${tableName}Controller {

    private final ${tableName}Service ${tableName1}Service;
    private final ${tableName}Converter ${tableName1}Converter;

    /**
    * 获取所有
    *
    * @return
    */
    @Operation(summary = "获取所有")
    @GetMapping("all")
    public R<List<${tableName}VO>> list() {
        List<${tableName}> list = ${tableName1}Service.list();
        return R.success(${tableName1}Converter.list2Vos(list));
    }

    /**
    * 分页获取
    *
    * @param query
    * @return
    */
    @Operation(summary = "分页获取")
    @GetMapping("page")
    public R<List<${tableName}VO>> list(GeneralQuery query) {
        IPage page = ${tableName1}Service.selectPage(query);
        page.setRecords(${tableName1}Converter.list2Vos(page.getRecords()));
        return R.page(page);
    }

    /**
    * 获取单个实体类
    *
    * @param id
    * @return
    */
    @Operation(summary = "获取单个实体类")
    @GetMapping("/{id}")
    public R<${tableName}VO> getById(@PathVariable Long id) {
        return R.success(${tableName1}Converter.entity2Vo(${tableName1}Service.getById(id)));
    }

    /**
    * 新增单个实体类
    *
    * @param dto
    * @return
    */
    @Operation(summary = "新增单个实体类")
    @PostMapping()
    public R add(@RequestBody ${tableName}DTO dto) {
        this.${tableName1}Service.save(${tableName1}Converter.dto2Entity(dto));
        return R.success();
    }

    /**
    * 修改单个实体类
    * @param dto
    * @return
    */
    @Operation(summary = "修改单个实体类")
    @PutMapping("{${pkField}}")
    public R update(@RequestBody ${tableName}DTO dto, @PathVariable Long ${pkField}) {
        this.${tableName1}Service.updateById(${tableName1}Converter.dto2Entity(dto), ${pkField});
        return R.success();
    }

    /**
    * 删除
    * @param id
    * @return
    */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        this.${tableName1}Service.removeById(id);
        return R.success();
    }

}